﻿#include "ssTexture2dRender.h"
#include "../material/ssMaterial.h"
#include "../../resmgr/ssResManager.h"


namespace StarSeeker
{

ssTexture2dRender::ssTexture2dRender()
{
	m_texCoord = 0;
}

ssTexture2dRender::~ssTexture2dRender()
{
	if (!m_pMesh)
	{
		SS_SAFE_DELETE(m_texCoord)
	}
}

bool ssTexture2dRender::SetTextureCoord( u8 numCoordLayer, u32 numFloat, const ssfloat* pt )
{
	if (!(m_VDF&SS_VDF_TEXCOORD_2F))
	{
		SS_SAFE_NEW(m_texCoord, ssVBOBind)
		if (m_texCoord->CreateFloat(pt, numFloat))
		{
			m_VDF |= SS_VDF_TEXCOORD_2F;
			m_texAttrib.BindData(2*numCoordLayer, SS_FLOAT, 0, 0, "inTex2dCoord");
			return true;
		}
		SS_SAFE_DELETE(m_texCoord)
	}
	return false;
}

void ssTexture2dRender::SetDataFromMesh(u8 numCoordLayer, const char* uri)
{
	ssMeshRender::SetDataFromMesh(uri);
	if (m_pMesh)
	{
		m_texCoord = m_pMesh->GetTexture2dVBO();
		if (m_texCoord)
		{
			m_VDF |= SS_VDF_TEXCOORD_2F;
			m_texAttrib.BindData(2*numCoordLayer, SS_FLOAT, 0, 0, "inTex2dCoord");
		}
	}
}

void ssTexture2dRender::OnRenderBegin()
{
	ssMeshRender::OnRenderBegin();
	if (m_VDF&SS_VDF_TEXCOORD_2F)
	{
		m_texCoord->Use();
		m_texAttrib.Use(ssMaterial::s_pShader);
	}
}

void ssTexture2dRender::OnRenderEnd()
{
	ssMeshRender::OnRenderEnd();
	if (m_VDF&SS_VDF_TEXCOORD_2F)
	{
		m_texCoord->Nonuse();
		m_texAttrib.Nonuse();
	}
}

}
